package com.web.controller;


import com.domain.Customer;
import com.domain.Employee;
import com.qo.CustomerQueryObject;
import com.service.CustomerService;
import com.qo.QueryObject;
import com.service.EmployeeService;
import com.service.SystemDictionaryItemService;
import com.util.JsonResult;
import com.github.pagehelper.PageInfo;
import com.util.UserContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private SystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private EmployeeService employeeService;


    //总客户列表
    @RequiresPermissions(value = {"customer:list","客户页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        //判断subject是否包含admin、Market_Manager 角色，如果有，可以查询所有的数据
        Subject subject = SecurityUtils.getSubject();
        if(!(subject.hasRole("admin")||subject.hasRole("Market_Manager"))){
            //如果没有，就只能看自己的数据
            qo.setSellerId(UserContext.getCurrentUser().getId());
        }
        PageInfo<Customer> result = customerService.query(qo);
        model.addAttribute("result", result);
        //查询销售人员下拉框的数据
        model.addAttribute("sellers", employeeService.selectByRoleSn(new String[]{"Market", "Market_Manager"}));
        return "customer/list";
    }

    //潜在客户列表
    @RequiresPermissions(value = {"customer:potentialList","潜在客户页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String potentialList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        //判断subject是否包含admin、Market_Manager 角色，如果有，可以查询所有的数据
        Subject subject = SecurityUtils.getSubject();
        if(!(subject.hasRole("admin")||subject.hasRole("Market_Manager"))){
            //如果没有，就只能看自己的数据
            qo.setSellerId(UserContext.getCurrentUser().getId());
        }
        //设置只查询潜在客户状态的数据
        qo.setStatus(Customer.STATUS_COMMON);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("result", pageInfo);

        //查询职业、来源下拉框的数据
        model.addAttribute("jobs", systemDictionaryItemService.selectByParentSn("job"));
        model.addAttribute("sources", systemDictionaryItemService.selectByParentSn("source"));
        //查询销售人员下拉框的数据
        model.addAttribute("sellers", employeeService.selectByRoleSn(new String[]{"Market", "Market_Manager"}));
        //查询交流方式下拉框的数据
        model.addAttribute("ccts", systemDictionaryItemService.selectByParentSn("communicationMethod"));

        return "customer/potentialList";
    }

    //客户池列表
    @RequiresPermissions(value = {"customer:poolList","客户池页面"},logical = Logical.OR)
    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        //判断subject是否包含admin、Market_Manager 角色，如果有，可以查询所有的数据
        Subject subject = SecurityUtils.getSubject();
        if(!(subject.hasRole("admin")||subject.hasRole("Market_Manager"))){
            //如果没有，就只能看自己的数据
            qo.setSellerId(UserContext.getCurrentUser().getId());
        }
        //设置只查询潜在客户状态的数据
        qo.setStatus(Customer.STATUS_POOL);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("result", pageInfo);
        //查询销售人员下拉框的数据
        model.addAttribute("sellers", employeeService.selectByRoleSn(new String[]{"Market", "Market_Manager"}));

        return "customer/poolList";
    }

    /*@RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete","客户删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }*/


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        if (customer.getId() != null) {
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonResult();
    }

    //修改状态
    @RequestMapping("/updateStatus")
    @RequiresPermissions(value = {"customer:updateStatus","客户修改状态"},logical = Logical.OR)
    @ResponseBody
    public JsonResult updateStatus(Customer customer){
        customerService.updateStatus(customer.getStatus(),customer.getId());
        return new JsonResult();
    }
}
